home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / mcomm540.zip / COMM.HPP < prev    next >
C/C++ Source or Header  |  1991-01-10  |  17KB  |  374 lines

  1.  
  2. /////////////////////////////////////////////////////////////////////////////
  3. //                                                                         //
  4. //  COMM.HPP -- header file for C++ programs using MCOMM async routines.   //
  5. //    Zortech C++ 2.0, Turbo C++ 1.00 version.                             //
  6. //                                                                         //
  7. //    Mike Dumdei,  6 Holly Lane,  Texarkana TX  75503    (c) 1989,1990    //
  8. //                                                                         //
  9. /////////////////////////////////////////////////////////////////////////////
  10.  
  11. #if !defined(COMM_HPP)
  12. #define COMM_HPP
  13.  
  14. #if !defined(MCOMM)
  15.   #define _C_ cdecl
  16.   #define _N_ near
  17.   #define _F_ far
  18.   #define MCOMM
  19. #endif
  20.  
  21. //////////////////////////////////////////////
  22. //  Return codes                            //
  23. //////////////////////////////////////////////
  24. #define     R_OK           0x0000
  25. #define     R_NOMEM        0x0001
  26. #define     R_BAUDERR      0x0002
  27. #define     R_PARITYERR    0x0004
  28. #define     R_DTABITERR    0x0008
  29. #define     R_STPBITERR    0x0010
  30. #define     R_IRQUSED      0x0020
  31. #define     R_VCTRUSED     0x0040
  32. #define     R_NOPORT       0x0080
  33. #define     R_PORTUSED     0x0100
  34. #define     R_UARTERR      0x0200
  35.  
  36. #define     R_TXERR        (-1)
  37.  
  38. //////////////////////////////////////////////
  39. //  Interrupt bit masks                     //
  40. //////////////////////////////////////////////
  41. #define     IRQ3           0x08
  42. #define     IRQ4           0x10
  43.  
  44. //////////////////////////////////////////////
  45. //  Vector numbers                          //
  46. //////////////////////////////////////////////
  47. #define     VCTR3          0x0b
  48. #define     VCTR4          0x0c
  49.  
  50. //////////////////////////////////////////////
  51. //  Standard Comm Ports                     //
  52. //   Note - due to the ability to overload  //
  53. //   the open call, COM1 and COM2 are def-  //
  54. //   ined differently in the C++ version.   //
  55. //////////////////////////////////////////////
  56. #define     COM1        0
  57. #define     COM2        1
  58.  
  59. //////////////////////////////////////////////
  60. //  Defines for async_regs function         //
  61. //////////////////////////////////////////////
  62. #define     RXreg       0
  63. #define     TXreg       0
  64. #define     IERreg      1
  65. #define     IIDreg      2
  66. #define     FCRreg      2
  67. #define     LCRreg      3
  68. #define     MCRreg      4
  69. #define     LSRreg      5
  70. #define     MSRreg      6
  71. #define     LObaud      0
  72. #define     HIbaud      1
  73.  
  74. //////////////////////////////////////////////
  75. //  Miscellaneous bit masks                 //
  76. //////////////////////////////////////////////
  77. #define     B_DTR        0x0001
  78. #define     B_RTS        0x0002
  79.  
  80. /////////////////////////////////////////////////////////////////////////
  81. //  Status bit masks                                                   //
  82. /////////////////////////////////////////////////////////////////////////
  83.   //  S T A T 2  //
  84. #define     B_XUSE       0x0001   // using XON/XOFF protocol bit
  85. #define     B_XRXD       0x0002   // XOFF received bit
  86. #define     B_XSENT      0x0004   // XOFF sent bit
  87. #define     B_TXEMPTY    0x0008   // nothing to transmit bit
  88. #define     B_CTS        0x0010   // clear to send bit
  89. #define     B_DSR        0x0020   // data set ready bit
  90. #define     B_FLOWHALT   0x0040   // flow halt indicator bit
  91. #define     B_MNTR_CD    0x0080   // monitor CD bit
  92.  
  93.   //  S T A T 1  //
  94. #define     B_RXOVF      0x0100   // rx buffer overflow indic bit
  95. #define     B_OVERRUN    0x0200   // char overrun error bit
  96. #define     B_PARITY     0x0400   // parity error bit
  97. #define     B_FRAMING    0x0800   // framing error bit
  98. #define     B_BREAK      0x1000   // line break signal detected
  99. #define     B_THRE       0x2000   // used in interrupt hdlr (always 0)
  100. #define     B_RXEMPTY    0x4000   // rx buffer empty bit
  101. #define     B_CD         0x8000   // carrier detect bit mask
  102.  
  103.   //  S T A T 3  //
  104. #define     B_IGNERR     0x0001   // discard chars with par, frm'g errs
  105. #define     B_16550      0x0002   // 16550 detected
  106. #define     B_FIFO       0x0004   // 16550 FIFOs enabled
  107. #define     B_ORGFIFO    0x0008   // FIFOs were on when port was opened
  108. #define     B_FARBUFFER  0x0010   // using FAR ring buffers
  109. #define     B_XUSET      0x0020   // using XON/XOFF on txmt side bit
  110. #define     B_XUSER      0x0040   // using XON/XOFF on recv side bit
  111. #define     B_XONANY     0x0080   // accept any char for an XON
  112.  
  113.  
  114. /////////////////////////////////////////////////////////////////////////
  115. //                                                                     //
  116. //  A s y n c P o r t   C l a s s                                      //
  117. //                                                                     //
  118. //                    * * *  W A R N I N G  * * *                      //
  119. //     This class must have the port variables byte aligned.  As       //
  120. //     written, ZTC 2.0 will byte align this structure without the     //
  121. //     need to direct it to pack structures.  If you are using         //
  122. //     a different implementation of C++ be sure this data is          //
  123. //     byte aligned by your compiler.                                  //
  124. /////////////////////////////////////////////////////////////////////////
  125.  
  126. class AsyncPort
  127. {
  128.       // port variables
  129.   private:
  130.     int ComBase;          // base port address of for comm port
  131.     char IRQMask;         // mask for IRQ used by this port
  132.     char Vector;          // interrupt vector used by this port
  133.     char BPDSstr[10];     // current baud,parity,data,stop parms
  134.     int RxSize;           // size of receive bufr
  135.     int TxSize;           // size of transmit bufr
  136.     int RingSeg;          // segment of rx/tx bufrs (leave 0 for NEAR)
  137.     int RingOfst;         // offset of rx/tx bufrs, alias TxTop
  138.     int RxTop;            // ptr to begin of rx bufr, alias TxBtm
  139.     int RxBtm;            // ptr to end of rx bufr (end + 1)
  140.     int TxIn;             // ptr to where next 'put' goes
  141.     int TxOut;            // ptr to where next tx'd comes from
  142.     int TxFree;           // remaining tx bufr space
  143.     int RxIn;             // pointer to where next rx'd goes
  144.     int RxOut;            // ptr to where next 'get' comes from
  145.     int RxFree;           // remaining bytes in rx bufr space
  146.     int BaudDvsr;         // baud rate divisor
  147.     int XoffTrip;         // send XOFF when this many bytes left in rxbuf
  148.     int XonTrip;          // send XON when this many bytes left in rxbuf
  149.     int OldVctrOfst;      // OFST address of original interrupt vector
  150.     int OldVctrSeg;       // SEG address of original interrupt vector
  151.     int OldDvsr;          // original value of Baud Divisor Regs
  152.     char Stat2;           // secondary status byte
  153.     char Stat1;           // primary status byte
  154.     char TxStat;          // determines when tx_intrpts need turned on
  155.     char MSRMask;         // slcts bits in MSR montrd for flow cntrl
  156.     char MSRVal;          // present value of modem status register
  157.     char Stat3;           // another flag, used for XONANY char
  158.     char PDSinfo;         // parity, # dta bits, # stop bits
  159.     char StripMask;       // mask for high bit stripping
  160.     char XTxRptInit;      // initialization value for XTxRpt below
  161.     char XTxRpt;          // countdown val befr repeat tx of XOFF
  162.     char TxImmedChar;     // char to be tx'd 1st opportunity
  163.     char HdlrUsed;        // assigned interrupt handler for this port
  164.     char Old8259Msk;      // original 8259 interrupt mask
  165.     char OldLCR;          // original Line Control Reg value
  166.     char OldMCR;          // original Modem Cntrl Reg value
  167.     char OldIER;          // original Interrupt Enable Reg value
  168.     int TxByteCnt;        // number bytes to send per tx interrupt
  169.     char FCRVal;          // FIFO control register data byte
  170.     char IERVal;          // interrupt enable register value
  171.  
  172.       // declaration of default ring buffer variables
  173.     static int DefRxBufSz;      // default receive ring buffer size
  174.     static int DefTxBufSz;      // default transmit buffer size
  175.     static int DefMemType;      // default to NEAR or FAR memory flag
  176.  
  177.   public:
  178.     AsyncPort();                    // constructors
  179.     AsyncPort(int RxBufSz, int TxBufSz, int UseFarMem = 0);
  180.  
  181.     ~AsyncPort();                   // destructor
  182.  
  183.                                     // allocate ring buffers function
  184.     int AllocBuffers(int RxBufSz, int TxBufSz, int UseFarMem = 0);
  185.     int FreeBuffers();              // free ring buffers function
  186.                                     // reset default buffers function
  187.     static void SetDefaultBuffers(int RxDefSz, int TxDefSz, int DefMem = 0);
  188.  
  189.                                     // open with full port definition
  190.     int Open(int ComBase, int IRQNbr, int InterruptVctr, char *Params = "");
  191.     int Open(int ComN, char *Params = ""); // open COM1 or COM2
  192.  
  193.     int Close();                    // close & leave DTR/RTS in pre-open state
  194.     int Close(int DTR, int RTS);    // close with option to force DTR/RTS
  195.  
  196.     void Release();                 // disable interrupt operation
  197.     void Restart();                 // resume interrupt mode of operation
  198.  
  199.     void PortAddress(int BaseAdrs); // set port base address
  200.     int PortAddress();              // get port base address
  201.  
  202.     int Tx(char Ch);                // transmit character
  203.     int Tx(char *String);           // transmit string
  204.     int Tx(char *Block, int NbrBytes); // transmit block
  205.  
  206.     int Rx();                       // receive character
  207.     int Rx(char *Block, int MaxNbrBytes); // receive block until maxbytes
  208.                                     // rx blk until delim or maxbytes
  209.     int Rx(char *Block, int MaxNbrBytes, char Delim, int IncludeDelim = 1);
  210.  
  211.     int Peek(int Offset = 0);       // peek receive buffer (index opt)
  212.  
  213.     int Status(int Mask = 0xffff);  // get port status (Stat1/Stat2)
  214.     void Reset();                   // reset parity,framing,char/bufr overrun
  215.     char Error(int Mask = 0x0f);    // test for parity,frame,char/bufr overrun
  216.  
  217.     int Params(char *Params);       // set port parameters
  218.     char *Params();                 // get port parameters
  219.  
  220.     void RxFlush();                 // flush receive buffer
  221.     void TxFlush();                 // flush transmit buffer
  222.  
  223.     void HardFlow(int Mask);        // set/reset hardware flow options
  224.  
  225.     void XFlow(int Mask);           // set/reset XON/XOFF flow control
  226.     char XFlow();                   // read XON/XOFF flow settings (bitmapped)
  227.                                     // set XON/XOFF trip levels
  228.     int XTrip(int XoffLevel, int XonLevel, int RepeatLevel);
  229.     void XoffReset();               // forced reset of XOFF received condition
  230.     void XoffSet();                 // simulate XOFF received
  231.     char XoffRxd();                 // test for XOFF received
  232.     char XoffTxd();                 // test for XOFF transmitted
  233.  
  234.     void Dtr(int Enable);           // set/reset DTR line
  235.     void Rts(int Enable);           // set/reset RTS line
  236.     char Msr();                     // get value of modem status register
  237.     char Cts();                     // get status of CTS line
  238.     char Dsr();                     // get status of DSR line
  239.     char Carrier();                 // get status of carrier detect line
  240.  
  241.     void Break(int Enable);         // set or reset breaking condition
  242.     char Break();                   // check if break signal received
  243.  
  244.     char Regs(int UARTreg, int Value = -1); // raw R/W of UART regs
  245.  
  246.     int RxLevel();                  // get number bytes in receive buffer
  247.     int TxAvailable();              // get bytes available in transmit buffer
  248.     char TxEmpty();                 // test for transmit buffer empty
  249.  
  250.     void RxMask(char Mask);         // set strip mask for incoming chars
  251.     void FlushBadChars(int Flag);   // set handling for chars with errs
  252.     char FlushBadChars();           // get setting chars with errs
  253. };
  254.  
  255. /////////////////////////////////////////////////////////////////////////
  256. //  E X T E R N A L   A S M   F U N C T I O N S                        //
  257. /////////////////////////////////////////////////////////////////////////
  258.   extern "C" {
  259. int async_open(AsyncPort *, int, int, int, char *); // open async port
  260. int async_close(AsyncPort *);              // close async port
  261. int async_tx(AsyncPort *, char);           // transmit character
  262. int async_txblk(AsyncPort *, char *, int); // transmit a block
  263. int async_rx(AsyncPort *);                 // receive a character
  264. int async_rxblk(AsyncPort *, char *, int); // receive a block
  265. int async_rxblkch(AsyncPort *, char *, int, char, int); // rx blk till char
  266. int async_stat(AsyncPort *, int);          // get stat1/stat2 bytes
  267. int async_setbpds(AsyncPort *, char *);    // change line parameters
  268. void async_rxflush(AsyncPort *);           // flush the receive buffer
  269. void async_txflush(AsyncPort *);           // flush the transmit buffer
  270. void async_msrflow(AsyncPort *, int);      // set MSR related flw cntrl
  271. void async_xflow(AsyncPort *, int);        // XON/OFF protocl on/off
  272. void async_xoffclr(AsyncPort *);           // manually clr XOFF rx'd
  273. void async_xoffset(AsyncPort *);           // manually set XOFF rx'd
  274. void async_dtr(AsyncPort *, int);          // DTR on/off
  275. void async_rts(AsyncPort *, int);          // RTS on/off
  276. void async_sndbrk(AsyncPort *, int);       // Set/clr tx modem brk sgnl
  277. void async_stop(AsyncPort *);              // halts interrupt operation
  278. void async_restart(AsyncPort *);           // re-init an already open port
  279. int async_peek(AsyncPort *, int);          // peek indexed char in rx buffer
  280. int async_regs(AsyncPort *, int, int);     // direct read/write of port regs
  281. void async_FIFOrxlvl(AsyncPort *, int);    // set rx FIFO trigger level
  282. void async_FIFOtxlvl(AsyncPort *, int);    // set max sent per tx intrpt
  283.   }
  284.  
  285.  
  286. /////////////////////////////////////////////////////////////////////////
  287. //  D E F I N I T I O N    O F   I N L I N E   F U N C T I O N S       //
  288. /////////////////////////////////////////////////////////////////////////
  289.  
  290. inline void AsyncPort::SetDefaultBuffers(int DefRxSz,int DefTxSz,int DefMem)
  291.  { DefRxBufSz = DefRxSz, DefTxBufSz = DefTxSz, DefMemType = DefMem; }
  292. inline void AsyncPort::PortAddress(int BaseAdrs)
  293.  { ComBase = BaseAdrs; }
  294. inline int AsyncPort::PortAddress()
  295.  { return ComBase; }
  296. inline void AsyncPort::Release()
  297.  { async_stop(this); }
  298. inline void AsyncPort::Restart()
  299.  { async_restart(this); }
  300. inline int AsyncPort::Tx(char Ch)
  301.  { return (async_tx(this, Ch)); }
  302. inline int AsyncPort::Tx(char *Block, int NbrBytes)
  303.  { return (async_txblk(this, Block, NbrBytes)); }
  304. inline int AsyncPort::Rx()
  305.  { return (async_rx(this)); }
  306. inline int AsyncPort::Rx(char *Block, int MaxNbrBytes)
  307.  { return (async_rxblk(this, Block, MaxNbrBytes)); }
  308. inline int AsyncPort::Rx(char *Block, int MaxNbrBytes, char Delim,
  309.  int IncludeDelim)
  310.  { return (async_rxblkch(this, Block, MaxNbrBytes, Delim, IncludeDelim)); }
  311. inline int AsyncPort::Peek(int Offset)
  312.  { return (async_peek(this, Offset)); }
  313. inline int AsyncPort::Status(int Mask)
  314.  { return (async_stat(this, Mask)); }
  315. inline void AsyncPort::Reset()
  316.  { Stat1 &= '\xe0'; }
  317. inline char AsyncPort::Error(int Mask)
  318.  { return (Stat1 & Mask); }
  319. inline int AsyncPort::Params(char *Params)
  320.  { return (async_setbpds(this, Params)); }
  321. inline char * AsyncPort::Params()
  322.  { return BPDSstr; }
  323. inline void AsyncPort::RxFlush()
  324.  { async_rxflush(this); }
  325. inline void AsyncPort::TxFlush()
  326.  { async_txflush(this); }
  327. inline void AsyncPort::HardFlow(int Mask)
  328.  { async_msrflow(this, Mask); }
  329. inline void AsyncPort::XFlow(int Mask)
  330.  { async_xflow(this, Mask); }
  331. inline char AsyncPort::XFlow()
  332.  { return (Stat3 & '\xe0'); }
  333. inline void AsyncPort::XoffReset()
  334.  { async_xoffclr(this); }
  335. inline void AsyncPort::XoffSet()
  336.  { async_xoffset(this); }
  337. inline char AsyncPort::XoffRxd()
  338.  { return (TxStat & '\x02'); }
  339. inline char AsyncPort::XoffTxd()
  340.  { return (Stat2 & '\x04'); }
  341. inline void AsyncPort::Dtr(int Enable)
  342.  { async_dtr(this, Enable); }
  343. inline void AsyncPort::Rts(int Enable)
  344.  { async_dtr(this, Enable); }
  345. inline char AsyncPort::Msr()
  346.  { return MSRVal; }
  347. inline char AsyncPort::Cts()
  348.  { return (MSRVal & '\x10'); }
  349. inline char AsyncPort::Dsr()
  350.  { return (MSRVal & '\x20'); }
  351. inline char AsyncPort::Carrier()
  352.  { return (MSRVal & '\x80'); }
  353. inline void AsyncPort::Break(int Enable)
  354.  { async_sndbrk(this, Enable); }
  355. inline char AsyncPort::Break()
  356.  { return (Stat1 & '\x10'); }
  357. inline char AsyncPort::Regs(int UARTreg, int Value)
  358.  { return async_regs(this, UARTreg, Value); }
  359. inline int AsyncPort::RxLevel()
  360.  { return (RxSize - RxFree); }
  361. inline int AsyncPort::TxAvailable()
  362.  { return (TxFree); }
  363. inline char AsyncPort::TxEmpty()
  364.  { return (TxStat & '\x08'); }
  365. inline void AsyncPort::RxMask(char Mask)
  366.  { StripMask = Mask; }
  367. inline void AsyncPort::FlushBadChars(int Flag)
  368.  { (Flag) ? (Stat3 |= 1) : (Stat3 &= '\xfe'); }
  369. inline char AsyncPort::FlushBadChars()
  370.  { return (Stat3 & 1); }
  371.      
  372. #endif          // COMM_HPP
  373.  
  374.